/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmAlumno;

import educar.abmUsuario.ModificarUserController;
import educar.controllers.PrincipalController;
import educar.db.JPA;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Matias
 */


public class AlumnoController implements ActionListener,MouseListener, WindowListener  {
   
    private PrincipalController principalController;    
    private AlumnoView alumnoView;
   
// <editor-fold defaultstate="collapsed" desc="Constructor">
    public AlumnoController (PrincipalController principalControl){        
        principalController = principalControl;
        alumnoView = new AlumnoView();
        alumnoView.setActionListener(this);
        alumnoView.setMouseListener(this);
        alumnoView.setWindowListener(this);
    }
    
    
 // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = 0;
        int y = 50;
        alumnoView.setBounds(x, y, screen.width, (screen.height)-90);
        alumnoView.setModeloTabla(listarAlumno());
        alumnoView.setVisible(true);
    }
// </editor-fold>   
    
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed"> 
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (alumnoView.getJbAgregar() == source) {
                AgregarAlumnoController agregarAlumnoController = new AgregarAlumnoController(this);
                agregarAlumnoController.setView();
        }
        
        if (alumnoView.getJbEliminar() == source) {
         EliminarAlumnoController eliminarAlumnoController = new EliminarAlumnoController();
            eliminarAlumnoController.eliminarRegistro(alumnoView.getModeloTabla());
        }    
        
        if (alumnoView.getJbBuscar() == source) {
           if (alumnoView.getJbBuscar() == source) {
            alumnoView.setModeloTabla(null);
            if (alumnoView.getCriterio().isEmpty()){
                alumnoView.setModeloTabla(listarAlumno());
            }else{  
                alumnoView.setModeloTabla(buscarAlumno(alumnoView.getCriterio()));
            }
        }
        }       
    }
        

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if ((alumnoView.getJTAlumnos() == source) && (e.getClickCount()==2)) {
        ModificarAlumnoController modificarAlumnoController = new ModificarAlumnoController(this);
            int fila = alumnoView.getJTAlumnos().getSelectedRow();
            Vector registro = (Vector) (alumnoView.getModeloTabla().getDataVector()).elementAt(fila);
            modificarAlumnoController.setView(registro, fila);
        }    
    }
    // </editor-fold> 

// <editor-fold defaultstate="collapsed" desc="Metodos auxiliares usados">    
    public void agregarATabla(String[] item){
        Object[] vector = new Object[5];
        vector[0] = (boolean) false;
        for (int i = 1; i < 5; i++){
            vector[i] = item[i-1];
        }
        alumnoView.getModeloTabla().addRow(vector);
    }

    public void actualizarFila(String[] item, int fila){
        alumnoView.getModeloTabla().setValueAt(false, fila, 0);
        for (int i = 1; i < 5; i++){
            alumnoView.getModeloTabla().setValueAt(item[i-1], fila, i);
        }
    }
    
    @Override
    public void windowClosing(WindowEvent e) {
        principalController.setView();
    }
    
    public DefaultTableModel listarAlumno() {
            JPA jpa = new JPA();
            Object[] vector = new Object[5];
            DefaultTableModel tabla = (DefaultTableModel) alumnoView.getJTAlumnos().getModel(); 
            ResultSet res = jpa.showAll("alumno");
            try {
                while (res.next()) {
                    vector[0] = false;
                    vector[1] = res.getObject(1);
                    vector[2] = res.getObject(2);
                    vector[3] = res.getObject(3);
                    vector[4] = res.getObject(4);
                    tabla.addRow(vector);
                }
            } catch (SQLException ex) {
            ex.printStackTrace();
            }
            return tabla;
    }
    
        public DefaultTableModel buscarAlumno(String alumnonameCriterio) {
            JPA jpa = new JPA();
            Object[] vector = new Object[5];
            DefaultTableModel tabla = (DefaultTableModel) alumnoView.getModeloTabla(); 
            ResultSet res = jpa.getByField("alumno", "apellidoYnombre", alumnonameCriterio);
            try {
                while (res.next()) {
                    vector[0] = false;
                    vector[1] = res.getObject(1);
                    vector[2] = res.getObject(2);
                    vector[3] = res.getObject(3);
                    vector[4] = res.getObject(4);
                    tabla.addRow(vector);
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return tabla;       
    }
    
// </editor-fold>    

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed no usadas"> 
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
// </editor-fold>    
    
}

